package com.tsing.cedap.rest.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tsing.cedap.rest.entity.OrderEntity;
import com.tsing.cedap.rest.vo.reqVo.OrderPageBySchoolReqVo;
import com.tsing.cedap.rest.vo.respVo.OrderRespVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 订单表 Mapper 接口
 * </p>
 *
 * @author TheTsing
 * @since 2022-01-26
 */
public interface OrderMapper extends BaseMapper<OrderEntity> {

    @Select("select o.id,cs.name courierStationName,eb.name expressBrandName,o.express_code,o.ware_type,o.weight,\n" +
            "o.name,o.phone,o.description,ca.name campusName,ra.address receivingAddressName,ra.name customerName,ra.phone customerPhone,\n" +
            "o.expected_time,o.tip,o.payment_method,o.order_time,o.receive_time,o.send_time,o.complete_time,o.cancel_time,o.status,o.change_reason,o.rider_id\n" +
            "from cedap_order o\n" +
            "inner join cedap_express_brand eb on o.express_brand_id=eb.id\n" +
            "inner join cedap_courier_station cs on eb.courier_station_id=cs.id\n" +
            "inner join cedap_user u on o.customer_id=u.id\n" +
            "inner join cedap_college c on u.college_id=c.id\n" +
            "inner join cedap_receiving_address ra on o.receiving_address_id=ra.id\n" +
            "inner join cedap_campus ca on ra.campus_id=ca.id\n" +
            "where o.id=#{id}")
    OrderRespVo getOrderById(@Param("id") String code);

    @Select("select o.id,cs.name courierStationName,eb.name expressBrandName,o.express_code,o.ware_type,o.weight,\n" +
            "o.name,o.phone,o.description,ca.name campusName,ra.address receivingAddressName,ra.name customerName,ra.phone customerPhone,\n" +
            "o.expected_time,o.tip,o.payment_method,o.order_time,o.receive_time,o.send_time,o.complete_time,o.cancel_time,o.status,o.change_reason,o.rider_id\n" +
            "from cedap_order o\n" +
            "inner join cedap_express_brand eb on o.express_brand_id=eb.id\n" +
            "inner join cedap_courier_station cs on eb.courier_station_id=cs.id\n" +
            "inner join cedap_user u on o.customer_id=u.id\n" +
            "inner join cedap_college c on u.college_id=c.id\n" +
            "inner join cedap_receiving_address ra on o.receiving_address_id=ra.id\n" +
            "inner join cedap_campus ca on ra.campus_id=ca.id\n" +
            "where o.customer_id=#{id} and o.status=#{status}\n" +
            "order by id desc")
    List<OrderRespVo> orderPage(IPage<OrderRespVo> page, @Param("id") String id, @Param("status") Integer status);

    @Select("<script>" +
            "select o.id,cs.name courierStationName,eb.name expressBrandName,o.express_code,o.ware_type,o.weight,\n" +
            "o.name,o.phone,o.description,ca.name campusName,ra.address receivingAddressName,ra.name customerName,ra.phone customerPhone,\n" +
            "o.expected_time,o.tip,o.payment_method,o.order_time,o.receive_time,o.send_time,o.complete_time,o.cancel_time,o.status,o.change_reason,o.rider_id\n" +
            "from cedap_order o\n" +
            "inner join cedap_express_brand eb on o.express_brand_id=eb.id\n" +
            "inner join cedap_courier_station cs on eb.courier_station_id=cs.id\n" +
            "inner join cedap_user u on o.customer_id=u.id\n" +
            "inner join cedap_college c on u.college_id=c.id\n" +
            "inner join cedap_receiving_address ra on o.receiving_address_id=ra.id\n" +
            "inner join cedap_campus ca on ra.campus_id=ca.id\n" +
            "<where>" +
            "<if test=\" reqVo.id !=null and reqVo.id !='' \">and o.id=#{reqVo.id}</if>" +
            "<if test=\" reqVo.customerPhone !=null and reqVo.customerPhone !='' \">and u.phone=#{reqVo.customerPhone}</if>" +
            "</where>" +
            "order by id desc" +
            "</script>")
    List<OrderRespVo> orderPageBySchool(IPage<OrderRespVo> page, @Param("reqVo") OrderPageBySchoolReqVo reqVo);

    @Select("select o.id,cs.name courierStationName,eb.name expressBrandName,o.express_code,o.ware_type,o.weight,\n" +
            "o.name,o.phone,o.description,ca.name campusName,ra.address receivingAddressName,ra.name customerName,ra.phone customerPhone,\n" +
            "o.expected_time,o.tip,o.payment_method,o.order_time,o.receive_time,o.send_time,o.complete_time,o.cancel_time,o.status,o.change_reason,o.rider_id\n" +
            "from cedap_order o\n" +
            "inner join cedap_express_brand eb on o.express_brand_id=eb.id\n" +
            "inner join cedap_courier_station cs on eb.courier_station_id=cs.id\n" +
            "inner join cedap_user u on o.customer_id=u.id\n" +
            "inner join cedap_college c on u.college_id=c.id\n" +
            "inner join cedap_receiving_address ra on o.receiving_address_id=ra.id\n" +
            "inner join cedap_campus ca on ra.campus_id=ca.id\n" +
            "where o.status=1 and o.customer_id <> #{userId} and ra.campus_id=#{campusId} and (rider_id = '' or rider_id is null)\n" +
            "order by o.expected_time asc")
    List<OrderRespVo> missedOrdersPage(IPage<OrderRespVo> page, @Param("campusId") String reqVoCode, @Param("userId") String id);


    @Select("select o.id,cs.name courierStationName,eb.name expressBrandName,o.express_code,o.ware_type,o.weight,\n" +
            "o.name,o.phone,o.description,ca.name campusName,ra.address receivingAddressName,ra.name customerName,ra.phone customerPhone,\n" +
            "o.expected_time,o.tip,o.payment_method,o.order_time,o.receive_time,o.send_time,o.complete_time,o.cancel_time,o.status,o.change_reason,o.rider_id\n" +
            "from cedap_order o\n" +
            "inner join cedap_express_brand eb on o.express_brand_id=eb.id\n" +
            "inner join cedap_courier_station cs on eb.courier_station_id=cs.id\n" +
            "inner join cedap_user u on o.customer_id=u.id\n" +
            "inner join cedap_college c on u.college_id=c.id\n" +
            "inner join cedap_receiving_address ra on o.receiving_address_id=ra.id\n" +
            "inner join cedap_campus ca on ra.campus_id=ca.id\n" +
            "where o.rider_id=#{id} and o.status=#{status}\n" +
            "order by id desc")
    List<OrderRespVo> riderOrderPage(IPage<OrderRespVo> page, @Param("id") String id, @Param("status") Integer status);

}
